<?php
/**
 * 蜗牛博客
 * 本文件内代码属于“蜗牛博客”项目，禁止修改再发布本程序源码，禁止去除页面底部的版权申明。所有版权保留
 * User: upliu
 * Email: 753073861@qq.com
 * QQ: 753073861
 * 程序主页：http://demo.upliu.net/snail-blog/
 */

require __DIR__ . '/init.php';

if (!is_installed()) {
    redirect_path('install.php');
}

$db = Db::getInstance();
$post = $db->query("select * from post where id=?", get("id"));
if (empty($post)) {
    snail_alert('文章不存在');
}

$category_name = $db->queryColumn("select name from category where id=?", $post['category_id']);
$author = $db->query("select id,nickname from user where id=?", $post['post_author_id']);

$rows = $db->queryAll('select post_id,tag_id from map_post_tag where post_id=?', $post['id']);
$tag_ids = array_column($rows, 'tag_id');
$rows = $db->queryAllWhereIn('select id,name from tag', 'id', $tag_ids);
$tag_name_map = array_column($rows, 'name', 'id');

$post['tag'] = implode('、', array_map(function($tag_id) use ($tag_name_map){
    return "<a href='?tag_id=$tag_id'>".hsc($tag_name_map[$tag_id])."</a>";
}, $tag_ids));

$comments_data = $db->getList('comment', array(
    'comment_post_id' => $post['id'],
    'comment_parent' => 0,
    'order by id desc'
), 10);

// @todo 回复评论

echo renderWithLayout('single', array(
    'post' => $post,
    'category_name' => $category_name,
    'author' => $author,
    'comments' => $comments_data['items'],
    'pagination' => $comments_data['pagination']['html'],
));